/**
 *  @author TalkTao
 * @description  接口封装
 */
import { usePiniaState, piniaStore } from "@/store/index";
import { REQUEST_TIMEOUT } from "../../env.config";
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, Method } from "axios";
import { Notify } from "vant";
export interface AxiosResponseProps {
  code?: number;
  status?: number;
  data?: any;
  datas?: any;
  msg?: string | null;
}
// 不用判断请求loading的路由
const ignoreLoadingUrls = ["v1/captchas", "v2/login", "v2/successInfo"];
const store = usePiniaState(piniaStore);
class HttpRequest {
  private readonly baseURL: string;
  private readonly withCredentials: boolean;
  private readonly timeout: number;
  constructor() {
    //获取当前环境的api地址
    this.baseURL = import.meta.env.VITE_BASE_URL as string;
    this.withCredentials = true;
    this.timeout = REQUEST_TIMEOUT;
  }
  //初始化get请求
  getInitConfig(): AxiosRequestConfig {
    return {
      baseURL: this.baseURL,
      withCredentials: this.withCredentials,
      timeout: this.timeout,
    };
  }
  interceptors(instance: AxiosInstance) {
    // 定义存放请求接口数组
    let requestList = [];
    const setLoadingToFalse = response => {
      requestList
        .filter(item => item.url == response.config.url && item.method == response.config.method)
        .forEach(item => (item.isLoading = false));

      //所有请求都加载完才让加载提示消失
      if (requestList.every(item => !item.isLoading)) store.isLoading = false;
    };

    instance.interceptors.request.use(
      (config: any) => {
        if (!ignoreLoadingUrls.includes(config.url)) {
          if (requestList.length < 10) {
            requestList.unshift({ ...config, isLoading: true });
          } else {
            requestList = [...requestList.slice(0, 9), { ...config, isLoading: true }];
          }
          store.isLoading = true;
        }
        return config;
      },
      error => {
        store.isLoading = false;
        Notify({ type: "warning", message: "请求数据错误" });
        Promise.reject(error + "请求错误");
      }
    );

    instance.interceptors.response.use(
      response => {
        setLoadingToFalse(response);
        return response.data;
      },
      error => {
        if (error.response?.status == 301) {
        }
        store.isLoading = false;
        console.log(error);
        setLoadingToFalse(error);
        Notify({ type: "warning", message: "请求数据错误" });
        return Promise.reject(error.response?.data);
      }
    );
  }
  request(): AxiosInstance {
    const instance = axios.create(this.getInitConfig());
    this.interceptors(instance);
    return instance;
  }
}
const http = new HttpRequest();
export default http.request();
